package com.m3958.firstgwt.server.dao;


import com.m3958.firstgwt.server.model.MenuItem;
import com.m3958.firstgwt.server.model.MenuLevel;
import com.m3958.firstgwt.shared.type.SmartConstants;



public class MenuItemChangeStrategy extends BaseModelChangeStrategy implements ModelChangeStrategy<MenuItem> {

	@Override
	public boolean extraPersistTask(MenuItem model){
		setInitCatAndLevel(model);
		return true;
	}

	@Override
	public boolean extraUpdateTask(MenuItem model,MenuItem newModel){
		setInitCatAndLevel(model);
		return true;
	}
	
	@Override
	public boolean extraRemoveTask(MenuItem model) {
		for(MenuLevel ml : model.getMenuLevels()){
			ml.getMenuitems().remove(model);
		}
		return true;
	}
	
	private boolean setInitCatAndLevel(MenuItem model){
		if(getReqPs() != null){
//			String cat = getReqPs().getStringValue(MenuItemField.MENUITEMCAT.getValue());
//			model.setMenuItemCat(MenuItemCategory.valueOf(cat));
			if(getReqPs().getRelationModelId() != SmartConstants.NONE_EXIST_MODEL_ID){
				MenuLevel ml = emp.get().find(MenuLevel.class, getReqPs().getRelationModelId());
				ml.getMenuitems().add(model);
			}
			return true;
		}else{
			return false;
		}
	}

	@Override
	public boolean afterPersist(MenuItem model) {
		return false;
	}
}

	
